<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Image Pyramids</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Image Pyramids </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../dd/dd7/tutorial_morph_lines_detection.html">Extract horizontal and vertical lines by using morphological operations</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../db/d8e/tutorial_threshold.html">Basic Thresholding Operations</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Use the OpenCV functions <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b> and <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff" title="Blurs an image and downsamples it. ">pyrDown()</a></b> to downsample or upsample a given image.</li>
</ul>
<h2>Theory </h2>
<dl class="section note"><dt>Note</dt><dd>The explanation below belongs to the book <b>Learning OpenCV</b> by Bradski and Kaehler.</dd></dl>
<ul>
<li>Usually we need to convert an image to a size different than its original. For this, there are two possible options:<ol type="1">
<li><em>Upsize</em> the image (zoom in) or</li>
<li><em>Downsize</em> it (zoom out).</li>
</ol>
</li>
<li>Although there is a <em>geometric transformation</em> function in OpenCV that -literally- resize an image (<b>resize</b> , which we will show in a future tutorial), in this section we analyze first the use of <b>Image Pyramids</b>, which are widely applied in a huge range of vision applications.</li>
</ul>
<h3>Image Pyramid</h3>
<ul>
<li>An image pyramid is a collection of images - all arising from a single original image - that are successively downsampled until some desired stopping point is reached.</li>
<li>There are two common kinds of image pyramids:<ul>
<li><b>Gaussian pyramid:</b> Used to downsample images</li>
<li><b>Laplacian pyramid:</b> Used to reconstruct an upsampled image from an image lower in the pyramid (with less resolution)</li>
</ul>
</li>
<li>In this tutorial we'll use the <em>Gaussian pyramid</em>.</li>
</ul>
<h4>Gaussian Pyramid</h4>
<ul>
<li><p class="startli">Imagine the pyramid as a set of layers in which the higher the layer, the smaller the size.</p>
<div class="image">
<img src="../../Pyramids_Tutorial_Pyramid_Theory.png" alt="Pyramids_Tutorial_Pyramid_Theory.png"/>
</div>
</li>
<li>Every layer is numbered from bottom to top, so layer \((i+1)\) (denoted as \(G_{i+1}\) is smaller than layer \(i\) ( \(G_{i}\)).</li>
<li><p class="startli">To produce layer \((i+1)\) in the Gaussian pyramid, we do the following:</p><ul>
<li>Convolve \(G_{i}\) with a Gaussian kernel:</li>
</ul>
<p class="formulaDsp">
\[\frac{1}{16} \begin{bmatrix} 1 &amp; 4 &amp; 6 &amp; 4 &amp; 1 \\ 4 &amp; 16 &amp; 24 &amp; 16 &amp; 4 \\ 6 &amp; 24 &amp; 36 &amp; 24 &amp; 6 \\ 4 &amp; 16 &amp; 24 &amp; 16 &amp; 4 \\ 1 &amp; 4 &amp; 6 &amp; 4 &amp; 1 \end{bmatrix}\]
</p>
<ul>
<li>Remove every even-numbered row and column.</li>
</ul>
</li>
<li>You can easily notice that the resulting image will be exactly one-quarter the area of its predecessor. Iterating this process on the input image \(G_{0}\) (original image) produces the entire pyramid.</li>
<li>The procedure above was useful to downsample an image. What if we want to make it bigger?: columns filled with zeros ( \(0 \))<ul>
<li>First, upsize the image to twice the original in each dimension, with the new even rows and</li>
<li>Perform a convolution with the same kernel shown above (multiplied by 4) to approximate the values of the "missing pixels"</li>
</ul>
</li>
<li>These two procedures (downsampling and upsampling as explained above) are implemented by the OpenCV functions <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b> and <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff" title="Blurs an image and downsamples it. ">pyrDown()</a></b> , as we will see in an example with the code below:</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>When we reduce the size of an image, we are actually <em>losing</em> information of the image.</dd></dl>
<h2>Code </h2>
<p>This tutorial code's is shown lines below.</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><p> You can also download it from <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp">here</a> </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;iostream&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* window_name = <span class="stringliteral">&quot;Pyramids Demo&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;\n Zoom In-Out demo \n &quot;</span></div><div class="line">            <span class="stringliteral">&quot;------------------  \n&quot;</span></div><div class="line">            <span class="stringliteral">&quot; * [i] -&gt; Zoom in   \n&quot;</span></div><div class="line">            <span class="stringliteral">&quot; * [o] -&gt; Zoom out  \n&quot;</span></div><div class="line">            <span class="stringliteral">&quot; * [ESC] -&gt; Close program \n&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* filename = argc &gt;=2 ? argv[1] : <span class="stringliteral">&quot;chicky_512.png&quot;</span>;</div><div class="line"></div><div class="line">    <span class="comment">// Loads an image</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( filename ) );</div><div class="line"></div><div class="line">    <span class="comment">// Check if image is loaded fine</span></div><div class="line">    <span class="keywordflow">if</span>(src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>()){</div><div class="line">        printf(<span class="stringliteral">&quot; Error opening image\n&quot;</span>);</div><div class="line">        printf(<span class="stringliteral">&quot; Program Arguments: [image_name -- default chicky_512.png] \n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, src );</div><div class="line">        <span class="keywordtype">char</span> c = (char)<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( c == 27 )</div><div class="line">        { <span class="keywordflow">break</span>; }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">pyrUp</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>*2, src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>*2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom In: Image x 2 \n&quot;</span> );</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">pyrDown</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>/2, src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>/2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom Out: Image / 2 \n&quot;</span> );</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><p> You can also download it from <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgProc/Pyramids/Pyramids.java">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>PyramidsRun {</div><div class="line"></div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> window_name = <span class="stringliteral">&quot;Pyramids Demo&quot;</span>;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        System.out.println(<span class="stringliteral">&quot;\n&quot;</span> +</div><div class="line">                <span class="stringliteral">&quot; Zoom In-Out demo    \n&quot;</span> +</div><div class="line">                <span class="stringliteral">&quot;------------------   \n&quot;</span> +</div><div class="line">                <span class="stringliteral">&quot; * [i] -&gt; Zoom [i]n  \n&quot;</span> +</div><div class="line">                <span class="stringliteral">&quot; * [o] -&gt; Zoom [o]ut \n&quot;</span> +</div><div class="line">                <span class="stringliteral">&quot; * [ESC] -&gt; Close program \n&quot;</span>);</div><div class="line"></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = ((args.length &gt; 0) ? args[0] : <span class="stringliteral">&quot;../data/chicky_512.png&quot;</span>);</div><div class="line"></div><div class="line">        <span class="comment">// Load the image</span></div><div class="line">        Mat src = Imgcodecs.imread(filename);</div><div class="line"></div><div class="line">        <span class="comment">// Check if image is loaded fine</span></div><div class="line">        <span class="keywordflow">if</span>( src.empty() ) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Error opening image!&quot;</span>);</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Program Arguments: [image_name -- default ../data/chicky_512.png] \n&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>){</div><div class="line">            HighGui.imshow( window_name, src );</div><div class="line">            <span class="keywordtype">char</span> c = (char) HighGui.waitKey(0);</div><div class="line">            c = Character.toLowerCase(c);</div><div class="line"></div><div class="line">            <span class="keywordflow">if</span>( c == 27 ){</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span>){</div><div class="line">                Imgproc.pyrUp( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()*2, src.rows()*2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom In: Image x 2&quot;</span> );</div><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span>){</div><div class="line">                Imgproc.pyrDown( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()/2, src.rows()/2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom Out: Image / 2&quot;</span> );</div><div class="line">            }</div><div class="line">        }</div><div class="line"></div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>Pyramids {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native library.</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> PyramidsRun().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><p> You can also download it from <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/imgProc/Pyramids/pyramids.py">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> sys</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>main(argv):</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&quot;&quot;&quot;</span></div><div class="line"><span class="stringliteral">    Zoom In-Out demo</span></div><div class="line"><span class="stringliteral">    ------------------</span></div><div class="line"><span class="stringliteral">    * [i] -&gt; Zoom [i]n</span></div><div class="line"><span class="stringliteral">    * [o] -&gt; Zoom [o]ut</span></div><div class="line"><span class="stringliteral">    * [ESC] -&gt; Close program</span></div><div class="line"><span class="stringliteral">    &quot;&quot;&quot;</span>)</div><div class="line">    </div><div class="line">    filename = argv[0] <span class="keywordflow">if</span> len(argv) &gt; 0 <span class="keywordflow">else</span> <span class="stringliteral">&#39;chicky_512.png&#39;</span></div><div class="line"></div><div class="line">    <span class="comment"># Load the image</span></div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(filename))</div><div class="line"></div><div class="line">    <span class="comment"># Check if image is loaded fine</span></div><div class="line">    <span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Error opening image!&#39;</span>)</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Usage: pyramids.py [image_name -- default ../data/chicky_512.png] \n&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div><div class="line">    </div><div class="line">    <span class="keywordflow">while</span> 1:</div><div class="line">        rows, cols, _channels = map(int, src.shape)</div><div class="line">        </div><div class="line">        <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Pyramids Demo&#39;</span>, src)</div><div class="line">        </div><div class="line">        k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> k == 27:</div><div class="line">            <span class="keywordflow">break</span></div><div class="line">            </div><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;i&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">cv.pyrUp</a>(src, dstsize=(2 * cols, 2 * rows))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom In: Image x 2&#39;</span>)</div><div class="line">            </div><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;o&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">cv.pyrDown</a>(src, dstsize=(cols // 2, rows // 2))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom Out: Image / 2&#39;</span>)</div><div class="line">            </div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga6b7fc1c1a8960438156912027b38f481">cv.destroyAllWindows</a>()</div><div class="line">    <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> __name__ == <span class="stringliteral">&quot;__main__&quot;</span>:</div><div class="line">    main(sys.argv[1:])</div></div><!-- fragment -->  </div> <h2>Explanation </h2>
<p>Let's check the general structure of the program:</p>
<h4>Load an image</h4>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* filename = argc &gt;=2 ? argv[1] : <span class="stringliteral">&quot;chicky_512.png&quot;</span>;</div><div class="line"></div><div class="line">    <span class="comment">// Loads an image</span></div><div class="line">    Mat src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( filename ) );</div><div class="line"></div><div class="line">    <span class="comment">// Check if image is loaded fine</span></div><div class="line">    <span class="keywordflow">if</span>(src.empty()){</div><div class="line">        printf(<span class="stringliteral">&quot; Error opening image\n&quot;</span>);</div><div class="line">        printf(<span class="stringliteral">&quot; Program Arguments: [image_name -- default chicky_512.png] \n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = ((args.length &gt; 0) ? args[0] : <span class="stringliteral">&quot;../data/chicky_512.png&quot;</span>);</div><div class="line"></div><div class="line">        <span class="comment">// Load the image</span></div><div class="line">        Mat src = Imgcodecs.imread(filename);</div><div class="line"></div><div class="line">        <span class="comment">// Check if image is loaded fine</span></div><div class="line">        <span class="keywordflow">if</span>( src.empty() ) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Error opening image!&quot;</span>);</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Program Arguments: [image_name -- default ../data/chicky_512.png] \n&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    filename = argv[0] <span class="keywordflow">if</span> len(argv) &gt; 0 <span class="keywordflow">else</span> <span class="stringliteral">&#39;chicky_512.png&#39;</span></div><div class="line"></div><div class="line">    <span class="comment"># Load the image</span></div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(filename))</div><div class="line"></div><div class="line">    <span class="comment"># Check if image is loaded fine</span></div><div class="line">    <span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Error opening image!&#39;</span>)</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Usage: pyramids.py [image_name -- default ../data/chicky_512.png] \n&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div></div><!-- fragment --> </div> <h4>Create window</h4>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, src );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            HighGui.imshow( window_name, src );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Pyramids Demo&#39;</span>, src)</div></div><!-- fragment --> </div> <h4>Loop</h4>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, src );</div><div class="line">        <span class="keywordtype">char</span> c = (char)<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( c == 27 )</div><div class="line">        { <span class="keywordflow">break</span>; }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">pyrUp</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols*2, src.rows*2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom In: Image x 2 \n&quot;</span> );</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">pyrDown</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols/2, src.rows/2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom Out: Image / 2 \n&quot;</span> );</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>){</div><div class="line">            HighGui.imshow( window_name, src );</div><div class="line">            <span class="keywordtype">char</span> c = (char) HighGui.waitKey(0);</div><div class="line">            c = Character.toLowerCase(c);</div><div class="line"></div><div class="line">            <span class="keywordflow">if</span>( c == 27 ){</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span>){</div><div class="line">                Imgproc.pyrUp( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()*2, src.rows()*2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom In: Image x 2&quot;</span> );</div><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span>){</div><div class="line">                Imgproc.pyrDown( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()/2, src.rows()/2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom Out: Image / 2&quot;</span> );</div><div class="line">            }</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">while</span> 1:</div><div class="line">        rows, cols, _channels = map(int, src.shape)</div><div class="line">        </div><div class="line">        <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Pyramids Demo&#39;</span>, src)</div><div class="line">        </div><div class="line">        k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> k == 27:</div><div class="line">            <span class="keywordflow">break</span></div><div class="line">            </div><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;i&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">cv.pyrUp</a>(src, dstsize=(2 * cols, 2 * rows))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom In: Image x 2&#39;</span>)</div><div class="line">            </div><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;o&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">cv.pyrDown</a>(src, dstsize=(cols // 2, rows // 2))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom Out: Image / 2&#39;</span>)</div><div class="line">            </div></div><!-- fragment --> </div> <p>Perform an infinite loop waiting for user input. Our program exits if the user presses <b>ESC</b>. Besides, it has two options:</p>
<ul>
<li><p class="startli"><b>Perform upsampling - Zoom 'i'n (after pressing 'i')</b></p>
<p class="startli">We use the function <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b> with three arguments:</p><ul>
<li><em>src</em>: The current and destination image (to be shown on screen, supposedly the double of the input image)</li>
<li><em>Size( tmp.cols*2, tmp.rows*2 )</em> : The destination size. Since we are upsampling, <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b> expects a size double than the input image (in this case <em>src</em>).</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">pyrUp</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols*2, src.rows*2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom In: Image x 2 \n&quot;</span> );</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;i&#39;</span>){</div><div class="line">                Imgproc.pyrUp( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()*2, src.rows()*2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom In: Image x 2&quot;</span> );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;i&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784">cv.pyrUp</a>(src, dstsize=(2 * cols, 2 * rows))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom In: Image x 2&#39;</span>)</div></div><!-- fragment --> </div> <ul>
<li><p class="startli"><b>Perform downsampling - Zoom 'o'ut (after pressing 'o')</b></p>
<p class="startli">We use the function <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff" title="Blurs an image and downsamples it. ">pyrDown()</a></b> with three arguments (similarly to <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b>):</p><ul>
<li><em>src</em>: The current and destination image (to be shown on screen, supposedly half the input image)</li>
<li><em>Size( tmp.cols/2, tmp.rows/2 )</em> : The destination size. Since we are downsampling, <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff" title="Blurs an image and downsamples it. ">pyrDown()</a></b> expects half the size the input image (in this case <em>src</em>).</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span> )</div><div class="line">        { <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">pyrDown</a>( src, src, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols/2, src.rows/2 ) );</div><div class="line">            printf( <span class="stringliteral">&quot;** Zoom Out: Image / 2 \n&quot;</span> );</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            }<span class="keywordflow">else</span> <span class="keywordflow">if</span>( c == <span class="charliteral">&#39;o&#39;</span>){</div><div class="line">                Imgproc.pyrDown( src, src, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( src.cols()/2, src.rows()/2 ) );</div><div class="line">                System.out.println( <span class="stringliteral">&quot;** Zoom Out: Image / 2&quot;</span> );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">elif</span> chr(k) == <span class="stringliteral">&#39;o&#39;</span>:</div><div class="line">            src = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff">cv.pyrDown</a>(src, dstsize=(cols // 2, rows // 2))</div><div class="line">            <span class="keywordflow">print</span> (<span class="stringliteral">&#39;** Zoom Out: Image / 2&#39;</span>)</div></div><!-- fragment --> </div> <p>Notice that it is important that the input image can be divided by a factor of two (in both dimensions). Otherwise, an error will be shown.</p>
<h2>Results </h2>
<ul>
<li><p class="startli">The program calls by default an image <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/data/chicky_512.png">chicky_512.png</a> that comes in the <code>samples/data</code> folder. Notice that this image is \(512 \times 512\), hence a downsample won't generate any error ( \(512 = 2^{9}\)). The original image is shown below:</p>
<div class="image">
<img src="../../Pyramids_Tutorial_Original_Image.jpg" alt="Pyramids_Tutorial_Original_Image.jpg"/>
</div>
</li>
<li><p class="startli">First we apply two successive <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff" title="Blurs an image and downsamples it. ">pyrDown()</a></b> operations by pressing 'd'. Our output is:</p>
<div class="image">
<img src="../../Pyramids_Tutorial_PyrDown_Result.jpg" alt="Pyramids_Tutorial_PyrDown_Result.jpg"/>
</div>
</li>
<li><p class="startli">Note that we should have lost some resolution due to the fact that we are diminishing the size of the image. This is evident after we apply <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784" title="Upsamples an image and then blurs it. ">pyrUp()</a></b> twice (by pressing 'u'). Our output is now:</p>
<div class="image">
<img src="../../Pyramids_Tutorial_PyrUp_Result.jpg" alt="Pyramids_Tutorial_PyrUp_Result.jpg"/>
</div>
 </li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
